﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;

namespace GroupMappedDiskSnapInExtension
{
    public partial class MappedDiskControl : UserControl
    {    
        private GroupPropertyPage groupPropertyPage;
        private DirectoryEntry entry;
       

        public MappedDiskControl()
        {
            InitializeComponent();
        }       

        internal MappedDiskControl(GroupPropertyPage groupPropertyPage):this()
        {            
            this.groupPropertyPage = groupPropertyPage;            
        }


        internal void RefreshData(Microsoft.ManagementConsole.SharedDataItem sharedDataItem)
        {           
            var dn = new DsObjectNames(sharedDataItem).DistinguishedName;

            entry = new DirectoryEntry(dn);
            LoadMappings();
            
        }

        private void LoadMappings()
        {
            mappingsList.Items.Clear();
            foreach(object item in entry.Properties["mappedDisk"])
            {
                string[] mapping = item.ToString().Split(':');
                ListViewItem listItem = new ListViewItem();
                listItem.Text = mapping[0] + ":";
                listItem.SubItems.Add(mapping[1]);

                mappingsList.Items.Add(listItem);
            }
        }

        private void addMappingButton_Click(object sender, EventArgs e)
        {
            string letter = "";
            string path = "";

            if(MappingEditor.Edit(ref letter, ref path))
            {
                var item = new ListViewItem();
                item.Text = letter;
                item.SubItems.Add(path);

                mappingsList.Items.Add(item);
                groupPropertyPage.Dirty = true; 
            }
        }

        public bool OnApply()
        {
            SaveMappings();

            groupPropertyPage.Dirty = false;

            return true;
        }

        private void SaveMappings()
        {
            entry.Properties["mappedDisk"].Clear();
      
            foreach(ListViewItem item in mappingsList.Items)
            {
                string line = string.Format("{0}{1}", item.Text, item.SubItems[1].Text);
                entry.Properties["mappedDisk"].Add(line);
            }

            entry.CommitChanges();
        }

        private void removeMappingButton_Click(object sender, EventArgs e)
        {
            if(mappingsList.SelectedItems.Count > 0)
            {
                mappingsList.SelectedItems[0].Remove();
                groupPropertyPage.Dirty = true;
            }
        }

        private void editButton_Click(object sender, EventArgs e)
        {
            if(mappingsList.SelectedItems.Count > 0)
            {
                ListViewItem selected = mappingsList.SelectedItems[0];
                string letter = selected.Text;
                string path = selected.SubItems[1].Text;

                if(MappingEditor.Edit(ref letter, ref path))
                {
                    selected.Text = letter;
                    selected.SubItems[1].Text = path;
                    groupPropertyPage.Dirty = true;
                }
            }
        }
    }
}
